﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Media;
using System.IO;

namespace AzureDriverExplorer_Client
{
    public class Comm
    {
        //private System.Windows.Media.ImageSource icon;

        public string LastNamePath(string oldName)
        {
            return Path.GetFileName(oldName);

            //string str_Name = string.Empty;
            //string[] str_Split = oldName.Split('\\');
            //for (int i = 0; i < str_Split.Length; i++)
            //    if (!str_Split[i].Equals(""))
            //        str_Name = str_Split[i];

            //return str_Name;

        }

        public string FirstPath(string oldName)
        {
            string str_Name = string.Empty;
            string[] str_Split = oldName.Split('\\');
            for (int i = 0; i < str_Split.Length; i++)
                if (!str_Split[i].Equals(""))
                    str_Name += str_Split[i] + '\\';

            return str_Name;
        }


        public static byte[] ReadFile(string filePath)
        {
            byte[] buffer = null;
            try
            {
                buffer = File.ReadAllBytes(filePath);
            }
            catch { }

            return buffer;
        }


        public decimal getWithOutDecimal(decimal number)
        {
            decimal result = 0;
            try
            {
                string[] strNumber = number.ToString().Split(Const.PontChar);
                result = decimal.Parse(strNumber[0]);

            }
            catch { }
            return result;
        }

        public byte[] getPaket(byte[] file, long beginner, long maxNumberbits)
        {
            byte[] paket = new byte[maxNumberbits];
            long count = 0;
            while (count < maxNumberbits)
            {
                paket[count] = file[beginner + count];

                count++;
            }
            return paket;

        }

        /// <summary>
        /// 最後のパケットを切ります
        /// </summary>
        /// <param name="file"></param>
        /// <param name="beginner"></param>
        /// <param name="maxNumberbits"></param>
        /// <returns></returns>
        public byte[] getLastPaket(byte[] file, long beginner, long maxNumberbits)
        {
            byte[] paket = new byte[file.Length - beginner];
            long count = beginner, paketCount = 0;
            while (count < file.Length)
            {
                paket[paketCount] = file[count];
                paketCount++;
                count++;
            }
            return paket;

        }

        public string CreateFolder(string path)
        {

            string Messenger = string.Empty;
            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
            }
            catch (Exception ex)
            {
                Messenger = ex.Message;
            }

            return Messenger;
        }

        public string renameOriginalName(string path)
        {
            string error = string.Empty;
            string NewNameExt = System.IO.Path.GetExtension(path);
            string NewName = path.Replace(NewNameExt, ".zip");
            try
            {
                System.IO.File.Move(NewName, path);

            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            return error;
        }



        /// <summary>
        /// Search Directory files
        /// </summary>
        /// <returns></returns>
        public List<string> GetFileTheAllDirectory(string DirectoryPath)
        {

            //Localpath
            DirectoryPath = DirectoryPath + (DirectoryPath.EndsWith(@"\") ? string.Empty : @"\");

            List<string> FileName = new List<string>();
            string[] vecDirectory = System.IO.Directory.GetDirectories(DirectoryPath);


            GetFileTheAllFiles(DirectoryPath, ref FileName);


            //Only Chirlend Path
            for (int i = 0; i < FileName.Count; i++)
            {
                FileName[i] = FileName[i].Replace(DirectoryPath, "");
            }


            return FileName;
        }

        /// <summary>
        /// Get Directory File
        /// </summary>
        /// <param name="path"></param>
        /// <param name="FileName"></param>
        public void GetFileTheAllFiles(string path, ref List<string> FileName)
        {
            string[] vecFiles = System.IO.Directory.GetFiles(@path);
            foreach (string file in vecFiles)
            {
                FileName.Add(file);
            }

            string[] vecDirectory = System.IO.Directory.GetDirectories(path);
            foreach (string Local in vecDirectory)
            {
                GetFileTheAllFiles(Local, ref FileName);

            }

        }

    }
}
